/*
 * @(#)AdhocQueryConfig.java
 * Date 2013-08-13
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.registry.config;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import us.hornerscorners.lamppost.exception.ServiceActionError;
import us.hornerscorners.lamppost.exception.SeverityType;
import us.hornerscorners.lamppost.exception.ValidationException;

import us.hornerscorners.lamppost.registry.constant.QueryName;


/**
 *
 * @author jim
 */
public class AdhocQueryConfig {

    /** Field description */
    private static final String ERROR_CODE = "CFG-ERR";

    /** Field description */
    private List<QueryMethodConfig> queryMethods;

    /**
     * Constructs ...
     *
     */
    public AdhocQueryConfig() {

        super();
    }

    /**
     * Method description
     *
     *
     * @param queryName
     *
     * @return
     */
    public QueryMethodConfig findMethod(QueryName queryName) {

        return findMethodByURN(queryName.getUUID());
    }

    /**
     * Method description
     *
     *
     * @param urn
     *
     * @return
     */
    public QueryMethodConfig findMethodByURN(String urn) {

        QueryMethodConfig result = null;

        for (QueryMethodConfig method : queryMethods) {

            if (method.getId().equals(urn)) {

                result = method;

                break;
            }
        }

        return result;
    }

    /**
     * Method description
     *
     *
     * @return
     */
    public List<QueryMethodConfig> getQueryMethods() {
        return queryMethods;
    }

    /**
     * Method description
     *
     *
     * @param queryMethods
     */
    public void setQueryMethods(List<QueryMethodConfig> queryMethods) {
        this.queryMethods = queryMethods;
    }

    /**
     * Method description
     *
     *
     * @throws ValidationException
     */
    public void validate() throws ValidationException {

        List<ServiceActionError> errors = new ArrayList<>();

        if (this.queryMethods.isEmpty() == false) {

            for (QueryMethodConfig queryMethod : this.queryMethods) {

                String methodName = queryMethod.getQueryName().getName();

                if (queryMethod.getParameters().isEmpty() == false) {

                    for (QueryParameterConfig paramConfig : queryMethod.getParameters()) {

                        String paramName = paramConfig.getSlotName().getCode();

                        if (StringUtils.isBlank(paramName)) {

                            paramName = String.format("%-UNKNOWN", methodName);

                            String msg =
                                String.format("Method [%s] has an unnamed parameter.",
                                              methodName);

                            errors.add(new ServiceActionError(ERROR_CODE, msg,
                                                              SeverityType.Error));
                        }

                        if (paramConfig.getDataType() == null) {

                            String msg =
                                String.format(
                                    "Method [%s] Param [%s] is missing datatype.",
                                    methodName, paramName);

                            errors.add(new ServiceActionError(ERROR_CODE, msg,
                                                              SeverityType.Error));

                        }

                        if (paramConfig.getSlotName() == null) {

                            String msg =
                                String.format(
                                    "Method [%s] Param [%s] is missing slotName.",
                                    methodName, paramName);

                            errors.add(new ServiceActionError(ERROR_CODE, msg,
                                                              SeverityType.Error));
                        }
                    }

                } else {

                    String msg = String.format("Method [%s] is missing parameters.",
                                               methodName);

                    errors.add(new ServiceActionError(ERROR_CODE, msg,
                                                      SeverityType.Error));
                }

            }

        } else {

            String msg = String.format("Configuration is blank.");

            errors.add(new ServiceActionError(ERROR_CODE, msg, SeverityType.Error));
        }

        if (errors.isEmpty() == false) {

            throw new ValidationException(getClass(), errors);
        }
    }
}
